<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\med_doctor;
use App\Models\users;
use App\Models\office;
use Illuminate\Database\Eloquent\SoftDeletes;


//预约挂号表
class registration extends Model
{
    use HasFactory;
    use SoftDeletes;
    protected $table='registration';
    protected $primaryKey='id';

    //关联患者
    public function users()
    {
        return $this->hasMany(users::class,'id','user_id')->select('id','name','id_card','phone');
    }

    //关联医生
    public function doctor()
    {
        return $this->hasMany(med_doctor::class,'id','doctor_id')->select('id','registration_fee');
    }

    //科室
    public function office()
    {
        return $this->hasMany(office::class,'id','office_id');
    }

    //查询
    public function show($limit,$name,$type)
    {

        //搜索姓名返回值
        if ($type!=''){
            return self::with('users','doctor','office')->where('type',$type)->paginate($limit);
        }
        //搜索类型返回值
        if ($name!=''){
            return self::with('users','doctor','office')
                ->whereHas('users',function ($res) use ($name) {
                $res->where('name','like',"%$name%");
            })->paginate($limit);
        }
        //正常返回值
        return self::with('users','doctor','office')->where('deleted_at',null)->paginate($limit);
    }

    //删除
    public function del($id)
    {
        return $this->find($id)->delete();
    }

}
